package com.home.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.home.model.Area;
import com.home.model.Fix;
import com.home.model.HouseAge;
import com.home.model.HouseType;
import com.home.model.HouseUse;
import com.home.model.Price;
import com.home.model.Rent;
import com.home.model.Zone;
import com.home.service.AreaService;
import com.home.service.FixService;
import com.home.service.HouseAgeService;
import com.home.service.HouseTypeService;
import com.home.service.HouseUseService;
import com.home.service.PriceService;
import com.home.service.RentService;
import com.home.service.ZoneService;

public class Cache {

	private HouseTypeService houseTypeService;

	private PriceService priceService;

	private ZoneService zoneService;

	private FixService fixService;

	private RentService rentService;

	private HouseAgeService houseAgeService;

	private AreaService areaService;

	private HouseUseService houseUseService;

	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public HouseAgeService getHouseAgeService() {
		return houseAgeService;
	}

	public void setHouseAgeService(HouseAgeService houseAgeService) {
		this.houseAgeService = houseAgeService;
	}

	public HouseTypeService getHouseTypeService() {
		return houseTypeService;
	}

	public void setHouseTypeService(HouseTypeService houseTypeService) {
		this.houseTypeService = houseTypeService;
	}

	public HouseUseService getHouseUseService() {
		return houseUseService;
	}

	public void setHouseUseService(HouseUseService houseUseService) {
		this.houseUseService = houseUseService;
	}

	public RentService getRentService() {
		return rentService;
	}

	public void setRentService(RentService rentService) {
		this.rentService = rentService;
	}

	public FixService getFixService() {
		return fixService;
	}

	public void setFixService(FixService fixService) {
		this.fixService = fixService;
	}

	public PriceService getPriceService() {
		return priceService;
	}

	public void setPriceService(PriceService priceService) {
		this.priceService = priceService;
	}

	public ZoneService getZoneService() {
		return zoneService;
	}

	public void setZoneService(ZoneService zoneService) {
		this.zoneService = zoneService;
	}

	private Cache() {
		init();
		initData();
	}

	private static Cache cache = new Cache();

	public static Cache getInstance() {
		return cache;
	}

	private List<HouseType> typeList = new ArrayList<HouseType>();

	private List<Price> priceList = new ArrayList<Price>();

	private List<Zone> zoneList = new ArrayList<Zone>();

	private List<Fix> fixList = new ArrayList<Fix>();

	private List<HouseAge> houseAgeList = new ArrayList<HouseAge>();

	private List<Area> areaList = new ArrayList<Area>();

	private List<HouseUse> houseUseList = new ArrayList<HouseUse>();

	private List<Rent> rentList = new ArrayList<Rent>();

	private HashMap<String, HouseType> houseTypeMap = new HashMap<String, HouseType>();

	private HashMap<String, Price> priceMap = new HashMap<String, Price>();

	private HashMap<String, Zone> zoneMap = new HashMap<String, Zone>();

	private HashMap<String, Fix> fixMap = new HashMap<String, Fix>();

	private HashMap<String, HouseAge> houseAgeMap = new HashMap<String, HouseAge>();

	private HashMap<String, Rent> rentMap = new HashMap<String, Rent>();

	private HashMap<String, HouseUse> houseUseMap = new HashMap<String, HouseUse>();

	private HashMap<String, Area> areaMap = new HashMap<String, Area>();

	private void init() {

		priceService = (PriceService) ApplicationContextUtils
				.getBean("priceService");
		zoneService = (ZoneService) ApplicationContextUtils
				.getBean("zoneService");
		fixService = (FixService) ApplicationContextUtils.getBean("fixService");
		houseTypeService = (HouseTypeService) ApplicationContextUtils
				.getBean("houseTypeService");
		areaService = (AreaService) ApplicationContextUtils
				.getBean("areaService");
		houseUseService = (HouseUseService) ApplicationContextUtils
				.getBean("houseUseService");
		houseAgeService = (HouseAgeService) ApplicationContextUtils
				.getBean("houseAgeService");
		rentService = (RentService) ApplicationContextUtils
				.getBean("rentService");

	}

	private void initData() {
		typeList = houseTypeService.getList();
		priceList = priceService.getList();
		zoneList = zoneService.getList();
		fixList = fixService.getList();
		houseAgeList = houseAgeService.getList();
		areaList = areaService.getList();
		houseUseList = houseUseService.getList();
		rentList = rentService.getList();
		for (HouseType t : typeList) {
			houseTypeMap.put(t.getCode(), t);
		}
		for (Price p : priceList) {
			priceMap.put(p.getCode(), p);
		}
		for (Zone z : zoneList) {
			zoneMap.put(z.getCode(), z);
		}
		for (Fix f : fixList) {
			fixMap.put(f.getCode(), f);
		}

		for (HouseUse hu : houseUseList) {
			houseUseMap.put(hu.getCode(), hu);
		}
		for (Area a : areaList) {
			areaMap.put(a.getCode(), a);
		}
		for (HouseAge ha : houseAgeList) {
			houseAgeMap.put(ha.getCode(), ha);
		}
		for (Rent r : rentList) {
			rentMap.put(r.getCode(), r);
		}
	}

	public List<HouseType> getType() {
		return typeList;
	}

	public List<Price> getPrice() {
		return priceList;
	}

	public List<Zone> getZone() {
		return zoneList;
	}

	public List getFix() {
		return fixList;
	}

	public HouseType getHouseType(String code) {
		return houseTypeMap.get(code);
	}

	public Price getPrice(String code) {
		return priceMap.get(code);
	}

	public Zone getZone(String code) {
		return zoneMap.get(code);
	}

	public Fix getFix(String code) {
		return fixMap.get(code);
	}
	
	
	public HouseAge getHouseAge(String code) {
		return houseAgeMap.get(code);
	}

	public Rent getRent(String code) {
		return rentMap.get(code);
	}

	public Area getArea(String code) {
		return areaMap.get(code);
	}
	
	public List<Area> getArea(){
		return areaList;
	}
	
	public List<Rent> getRent(){
		return rentList;
	}
	
	public List<HouseAge> getHouseAge(){
		return houseAgeList;
	}
	
	
	public List<HouseUse> getHouseUse(){
		return houseUseList;
	}

	public HouseUse getHouseUse(String code) {
		return houseUseMap.get(code);
	}

	public void refreshHouseType() {
		typeList = houseTypeService.getList();
		HashMap<String, HouseType> map = new HashMap<String, HouseType>();
		for (HouseType t : typeList) {
			map.put(t.getCode(), t);
		}
		houseTypeMap = map;
	}

	public void refreshZone() {
		zoneList = zoneService.getList();
		HashMap<String, Zone> map = new HashMap<String, Zone>();
		for (Zone z : zoneList) {
			map.put(z.getCode(), z);
		}
		zoneMap = map;
	}

	public void refreshFix() {
		fixList = fixService.getList();
		HashMap<String, Fix> map = new HashMap<String, Fix>();
		for (Fix f : fixList) {
			map.put(f.getCode(), f);
		}
		fixMap = map;
	}

	public void refreshPrice() {
		priceList = priceService.getList();
		HashMap<String, Price> map = new HashMap<String, Price>();
		for (Price p : priceList) {
			map.put(p.getCode(), p);
		}
		priceMap = map;
	}

	public void refreshHouseAge() {
		houseAgeList = houseAgeService.getList();
		HashMap<String, HouseAge> map = new HashMap<String, HouseAge>();
		for (HouseAge ha : houseAgeList) {
			map.put(ha.getCode(), ha);
		}
		houseAgeMap = map;
	}

	public void refreshHouseUse() {
		houseUseList = houseUseService.getList();
		HashMap<String, HouseUse> map = new HashMap<String, HouseUse>();
		for (HouseUse hu : houseUseList) {
			map.put(hu.getCode(), hu);
		}
		houseUseMap = map;
	}

	public void refreshArea() {
		areaList = areaService.getList();
		HashMap<String, Area> map = new HashMap<String, Area>();
		for (Area a : areaList) {
			map.put(a.getCode(), a);
		}
		areaMap = map;
	}

	public void refreshRent() {
		rentList = rentService.getList();
		HashMap<String, Rent> map = new HashMap<String, Rent>();
		for (Rent r : rentList) {
			map.put(r.getCode(), r);
		}
		rentMap = map;
	}

}
